<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
	</head>
	<body>
		<script>
			// WeakRef 允许你持有对象的一个弱引用，这意味着当对象不再被任何强引用所引用时，垃圾回收器可以自由地回收该对象。
			class Resource {
				constructor(data) {
					this.data = data;
				}
			}

			const resource = new Resource("some data");
			const weakRef = new WeakRef(resource);
			console.log(weakRef.deref()); // Logs the Resource object

			// Clearing the strong reference.
			resource = null;
			// After garbage collection, the weakRef should be null.
			setTimeout(() => {
				console.log(weakRef.deref()); // Logs "undefined" if garbage collected
			}, 0);
		</script>
	</body>
</html>
